from django.contrib import admin
from apps.keys_management.models import RootKey, KeyAuditLog


@admin.register(RootKey)
class RootKeyAdmin(admin.ModelAdmin):
    list_display = ['app_id', 'key_id', 'key_type', 'key_length', 'version', 'status', 'created_at', 'expired_at']
    list_filter = ['key_type', 'status', 'created_at']
    search_fields = ['key_id', 'description']
    readonly_fields = ['key_id', 'key', 'version', 'created_at', 'updated_at']
    list_per_page = 20

    fieldsets = (
        ('基本信息', {
            'fields': ('app_id', 'key_id', 'key', 'key_type', 'key_length', 'description')
        }),
        ('版本信息', {
            'fields': ('version', 'status')
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at', 'expired_at')
        }),
        ('操作信息', {
            'fields': ('create_by', 'update_by', 'is_delete')
        }),
    )


@admin.register(KeyAuditLog)
class KeyAuditLogAdmin(admin.ModelAdmin):
    list_display = ['key_id', 'operation', 'operator', 'operation_time', 'ip_address']
    list_filter = ['operation', 'operation_time']
    search_fields = ['key_id', 'operator', 'details']
    readonly_fields = ['operation_time']
    list_per_page = 20

    fieldsets = (
        ('操作信息', {
            'fields': ('key_id', 'operation', 'operator')
        }),
        ('环境信息', {
            'fields': ('ip_address', 'user_agent')
        }),
        ('详细信息', {
            'fields': ('details', 'operation_time', 'expired_at')
        }),
    )
